﻿/*
 * Author: Thomas Mattsson
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Final_Project.CustomerData;

namespace Final_Project
{
    /// <summary>
    /// This class represent the main view of the program.
    /// </summary>
    public partial class MainForm : Form
    {
        // Local variables
        private CustomerManager manager;
        
        /// <summary>
        /// Default constructor.
        /// Creates the main window.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            manager = new CustomerManager();
            InitializeGUI();
        }


        /// <summary>
        /// Initialize the different gui components.
        /// </summary>
        private void InitializeGUI()
        {
            UpdateCustomerList();
        }


        /// <summary>
        /// Event-handler for the Add customer button.
        /// </summary>
        /// <param name="sender">Reference to the add customer button.</param>
        /// <param name="e">Additional event information.</param>
        private void btnAddCustomer_Click(object sender, EventArgs e)
        {
            CustomerForm frmCustomer = new CustomerForm();
            if (frmCustomer.ShowDialog() == DialogResult.OK)
            {
                manager.AddCustomer(frmCustomer.CustomerData);
                UpdateCustomerList();
            }
        }


        /// <summary>
        /// This method updates the listbox with all the current customers
        /// in CustomerManagers list of customers.
        /// </summary>
        private void UpdateCustomerList()
        {
            lboxShowCustomer.Items.Clear();
            string[] customerStrings = manager.GetCustomerStrings();
            lboxShowCustomer.Items.AddRange(customerStrings);
        }


        /// <summary>
        /// Event-handler for the Remove customer button.
        /// </summary>
        /// <param name="sender">A reference to the remove customer button.</param>
        /// <param name="e">Additional event information.</param>
        private void btnRemoveCustomer_Click(object sender, EventArgs e)
        {
            int index = lboxShowCustomer.SelectedIndex;
            if (index != -1)
            {
                DialogResult result = MessageBox.Show("Ta bort vald kund?",
               "Är du säker?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    manager.RemoveCustomer(index);
                    UpdateCustomerList();
                }
            }
        }


        /// <summary>
        /// Event-handler for the edit customer button.
        /// </summary>
        /// <param name="sender">A reference to the edit customer button.</param>
        /// <param name="e">Additional event information.</param>
        private void btnEditCustomer_Click(object sender, EventArgs e)
        {
            int index = lboxShowCustomer.SelectedIndex;
            if (index != -1)
            {
                CustomerForm frmCustomer = new CustomerForm();
                frmCustomer.CustomerData = manager.GetCustomer(index);
                Customer backup;

                if (manager.GetCustomer(index) is InvoiceCustomer)
                {
                    backup = new InvoiceCustomer((InvoiceCustomer)manager.GetCustomer(index));
                }
                else
                {
                    backup = new Customer(manager.GetCustomer(index));
                }
                frmCustomer.UpdateGUI();

                DialogResult result = frmCustomer.ShowDialog();

                if (result == DialogResult.OK)
                {
                    manager.UpdateCustomer(index, frmCustomer.CustomerData);
                    UpdateCustomerList();
                }
                else if (result == DialogResult.Cancel)
                {
                    manager.UpdateCustomer(index, backup);
                }
            }
        }
    }
}
